import Server from '@/config/server';
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  dataLists: [],
  Fields: [],
};
const actions = {
  // 任务监控列表
  getLists({ commit }, page) {
    request.get(Server.action.timing_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setLists', response.data.result.list);
          const { fields } = response.data.result;
          fields.push({
            title: '操作',
            align: 'center',
            width: 120,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
            key: 'action',
          });
          fields[0].width = 60;
          // fields[7].width = 130
          fields.forEach((item, i) => {
            item.dataIndex = item.key;
            item.align = 'center';
            item.scopedSlots = { customRender: item.key };
            if (i < fields.length - 2) {
              item.width = item.width || 130;
            }
          });
          commit('setFields', fields);
        } else {

        }
      });
  },
  // 添加
  dataAdd({ commit }, page) {
    commit('setErrorCode', false);
    request.post(Server.action.timing_add, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else if (response.status === 422) {
          showErrors(response.data.errors);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 编辑
  dataEdit({ commit }, page) {
    commit('setErrorCode', false);
    request.post(Server.action.timing_update, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // ID删除 > 参数为：[1,2,3,4,5,6]
  dataDel({ commit }, ids) {
    console.log(ids);
    request.get(Server.action.record_delete, { params: { id: ids } })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess('删除成功');
        } else {
          showError(response.data.message);
        }
      });
  },
  //   // 批量删除 > 参数为：[1,2,3,4,5,6]
  //   dataDel({ commit }, ids) {
  //     request.get(Server.action.record_delete, { params: ids })
  //       .then((response) => {
  //         console.log(response);
  //         if (response.data.errorCode === 0) {
  //           showSuccess(response.data.result);
  //         } else {
  //           showError(response.data.message);
  //         }
  //       });
  //   },
  // 批量删除 > 参数为：[1,2,3,4,5,6]
  // dataDel({commit}, ids) {
  //   request.get(Server.action.timing_delete ,{ params: ids })
  //   .then(response => {
  //     console.log(response);
  //     if (response.data.errorCode === 0) {
  //       showSuccess(response.data.result)
  //     } else {
  //       showError( response.data.message )
  //     }
  //   })
  // },

  // 执行记录列表
  getRecordLists({ commit }, page) {
    request.get(Server.action.record_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setLists', response.data.result.list);
          const { fields } = response.data.result;
          fields[0].width = 60;
          fields.forEach((item, i) => {
            fields[i].dataIndex = fields[i].key;
            fields[i].align = 'center';
            if (i < fields.length - 1) {
              fields[i].width = item.width || 120;
            }
          });
          commit('setFields', fields);
        } else {

        }
      });
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
  },
  setLists(state, lists) {
	    state.dataLists = lists;
  },
  setFields(state, lists) {
	    state.Fields = lists;
  },
};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
